tibco.admin.Main.loadResource("tibco/admin/deployment/jss/deployJSS.xml", "tibco.admin", "deployJSS_40", "ljss");

jsx3.lang.Package.definePackage("tibco.admin.deployment", function (deploy) {
});

/**
**/
jsx3.lang.Class.defineClass(
        'tibco.admin.deployment.Deploy',
        jsx3.lang.Object,
        [tibco.admin.plugin.SelectPerspectiveInterface],
        function(plugin) {
            plugin.instance = null;
            plugin.prototype.buttonsToLoad = new Object();
            plugin.prototype.envId = null;

            plugin.prototype.subscribed = false;

            plugin.prototype.init = function() {
                this.jsxsuper();
            }

            plugin.prototype.getNamespace = function() {
                return "tibco.admin";
            }

            plugin.prototype.initPlugin = function() {
            }

            plugin.getInstance = function() {
                if(!this.instance) {
                    this.instance = new tibco.admin.deployment.Deploy();
                }
                return this.instance;
            }

            plugin.prototype.getIconPath = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@addin@Welcome Deploy Icon");
            }

            plugin.prototype.getDescription = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@@welPageDepEnvContent_00");
            }

            plugin.prototype.getSelectText = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@lblSelectText_40");
            }

            plugin.prototype.getSectionTitle = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@lblDeployToTitle_40", ((this.envName)?this.envName:""));
            }

            plugin.prototype.loadStage2Component = function(parent, id) {
                this.envId = id;
                this.envName = this.select.getText();
                this.tabbedPane = new com.gix.gui.PerspectiveTabbedPane("tabpaneDeployment_40", 0, 0, "100%", "100%", null, this.getSectionTitle());
                parent.setChild(this.tabbedPane);
                this.tabbedPane.getChild(0).setRows("0,*",true);
                var me = this;
                tibco.admin.Main.loadUIResourceAsync(this.tabbedPane.getContentPane(), this.getNamespace(), "tibco/admin/deployment/ui/deployment.xml", me, me.tabbedPaneLoaded);
            }

            plugin.prototype.tabbedPaneLoaded = function(objEvent) {
                if(objEvent.status == "SUCCESS") {
                    this.block = objEvent.obj;
                    var me = this;
                    this.block.handler = me;
                    // load assemlby list
                    tibco.admin.Main.loadUIResourceAsync(this.block.getDescendantOfName("paneSAList40"), "tibco.admin", "tibco/admin/deployment/saList/ui/serviceAssemblyList.xml", me, me.serviceAssemblyListLoaded);
                }
                else {
                    if(objEvent.exception)
                    {
                        tibco.admin.Main.logException(objEvent.exception, objEvent.message);
                    }
                    else {
                        jsx3.log(objEvent.message);
                    }
                }
            }

            plugin.prototype.getComponentPath = function() {
                return "tibco/admin/deployment/component.xml"
            }

            plugin.prototype.loadComponent = function(parent) {
                try {
                    var objService = tibco.admin.Main.loadService("tibco.admin", "tibco/admin/environments/rules/getEnvironments.xml", "getEnvironments");
                    objService.setEndpointURL(tibco.admin.util.ServiceHelper.getEnvironmentEndpointURL());
                    objService.parentBlock = parent;
                    //subscribe and call
                    var me = this;
                    objService.subscribe(jsx3.net.Service.ON_SUCCESS, me, me.ongetEnvironmentsSuccess);
                    objService.doCall();
                }
                catch(ex) {
                    tibco.admin.Main.logException(ex, "Error in tibco.admin.environments.Environments.loadComponent");
                    throw new jsx3.lang.Exception("Error getting environments", jsx3.NativeError.wrap(ex));
                }
            }

            /**
             * load the drop list to select environment list.
             *
             */
            plugin.prototype.ongetEnvironmentsSuccess = function(objEvent) {
                var welcome = tibco.admin.Main.getServer().getJSXByName("blkWelcomePage_00");
                if (welcome)
                {
                    var docEnv = tibco.admin.Main.getServer().getCache().getDocument("getEnvironments_300");
                    welcome.setEnvListInside(this, docEnv);
                }

                var parent = objEvent.target.parentBlock;
                var me = this;
                tibco.admin.Main.loadUIResourceAsync(parent, this.getNamespace(), "tibco/admin/deployment/ui/selectEnvironment.xml", me, me.selectLoaded);
            }

            plugin.prototype.selectLoaded = function(objEvent) {
                // Use a hack way to customize the select box look like
                var selEnvironment = tibco.admin.Main.getServer().getJSXByName("selEnvironmentSelect_30");
                var selEnvironmentHTML = selEnvironment.getRendered();
                selEnvironmentHTML.firstChild.firstChild.style.paddingTop = 0;
                selEnvironmentHTML.style.backgroundImage = tibco.admin.Main.getServer().getDynamicProperty("@addin@Arrow For Header");
                selEnvironment.setDisplay(jsx3.gui.Block.DISPLAYBLOCK,true);

                if(objEvent.status == "SUCCESS") {
                    this.select = objEvent.obj.getChild(0);
                    var selXML = this.select.getXML();
                    var nodes = selXML.selectNodes("//record");

                    // if there is only one environment, then make it as workspace
                    // else, check the defalut worksapce variable.
                    var curJsxId = null;
                    if(nodes.size() == 1) {
                        curJsxId = nodes.getItem(0).getAttribute("jsxid")
                    }
                    else if (this.callbackTarget.workEnvironment) {
                        curJsxId = this.callbackTarget.workEnvironment;
                    }

                    if (curJsxId && selXML.selectSingleNode("//record[@jsxid='" + curJsxId + "']")) {
                        this.select.setValue(curJsxId);
                        this.itemSelected(curJsxId);
                    }
                }
                else {
                    if(objEvent.exception)
                    {
                        tibco.admin.Main.logException(objEvent.exception, objEvent.message);
                    }
                    else {
                        jsx3.log(objEvent.message);
                    }
                }
            }

            plugin.prototype.getSecondarySelectionText = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@lblSecondarySelectionText_40");
            }

            plugin.prototype.itemSelected = function(id) {
                // make a publish a message with the "Choose Environment" selectbox changed.
                var msg = new Object();
                msg.envID = id;
                msg.envName = tibco.admin.Main.getServer().getCache().
                        getDocument("getEnvironments_300").selectSingleNode("//record[@jsxid=" + id + "]").getAttribute("name");
                msg.subject = tibco.admin.deployment.EventID.ENVIRONMENT_SELECTED;
                tibco.admin.PubSub.publish(msg);
                this.notifyParent(id);
            }

            /**
             * load the component to the right pane if the component has not been loaded.
             */
            plugin.prototype.loadDetailsPane = function(objEvent) {
                if(!this.block)
                    return
                var paneSADetails = this.block.getDescendantOfName("paneSADetails_40")
                if(objEvent.entityID) {
                    this.block.getDescendantOfName("paneBanner_40").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                    this.block.getDescendantOfName("blkSAContent_40").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);

                    this.block.getDescendantOfName("lblSASelected_40").setText(objEvent.name, true);
                    this.block.getDescendantOfName("blkSAContent_40").repaint();
                    // Load detail pane once.
                    if(paneSADetails.getChildren().length == 0) {
                        var me = this;
                        tibco.admin.Main.loadUIResourceAsync(paneSADetails, "tibco.admin", "tibco/admin/deployment/sa/ui/details.xml", me, me.detailsLoaded, true);
                    }
                }
                else {
                    this.block.getDescendantOfName("paneBanner_40").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, false);
                    this.block.getDescendantOfName("blkSAContent_40").setDisplay(jsx3.gui.Block.DISPLAYNONE, false);
                    this.block.getDescendantOfName("panePlaceHodler_40").repaint();
                }
            }

            plugin.prototype.subscribeAll = function() {
                if(!this.subscribed) {
                    var me = this;
                    tibco.admin.PubSub.subscribe(tibco.admin.deployment.saList.EventID.SERVICEASSEMBLY_ROW_SELECTED, me, me.loadDetailsPane);
                    this.subscribed = true;
                }
            };

            plugin.prototype.unsubscribeAll = function() {
                var me = this;
                tibco.admin.PubSub.unsubscribe(tibco.admin.deployment.saList.EventID.SERVICEASSEMBLY_ROW_SELECTED, me);
                this.subscribed = false;
            }

            plugin.prototype.serviceAssemblyListLoaded = function(objEvent) {
                var me = this;
                this.childList = objEvent.obj.handler;
                this.childList.handler = me;
                // objEvent.obj is root element of the new loaded component
                // clear the event published last time
                tibco.admin.PubSub.publish({subject:tibco.admin.deployment.saList.EventID.SERVICEASSEMBLY_ROW_SELECTED, entityID:null});

                this.subscribeAll();
            }

            plugin.prototype.detailsLoaded = function(objEvent) {
                var me = this;
                this.childDetails = objEvent.obj.handler;
                this.childDetails.handler = me;
            }

            plugin.prototype.onDestroy = function() {
                this.unsubscribeAll();
                this.block = null;
            }

            plugin.prototype.unload = function() {
                tibco.admin.deployment.sa.Details.getInstance().unload();
                this.onAfterComponentUnloaded();
            }

            plugin.prototype.destroyPlugin = function() {
                tibco.admin.deployment.sa.Details.getInstance().destroyPlugin();
            }

            plugin.prototype.beginEdit = function(objPlugin) {
                this.childList.beginEdit();
            }

            plugin.prototype.endEdit = function(objPlugin) {
                if(!tibco.admin.deployment.sa.Details.getInstance().isInEditMode()) {
                    this.childList.endEdit();
                }
            }

            plugin.prototype.isInEditMode = function() {
                if(this.isEditMode) {
                    return true;
                }
                return tibco.admin.deployment.sa.Details.getInstance().isInEditMode();
            }

            plugin.prototype.allowComponentUnload = function(target, fctnYes, fctnNo, args) {
                var obj = new Object();
                obj.target = target;
                obj.fctnYes = fctnYes;
                obj.fctnNo = fctnNo;
                obj.args = args;
                var me = this;
                if(tibco.admin.deployment.sa.Details)
                    tibco.admin.deployment.sa.Details.getInstance().allowComponentUnload(me, me.detailsAllowsUnload, me.detailsDisallowsUnload, obj);
                else
                    this.detailsAllowsUnload(obj);
            };

            plugin.prototype.detailsAllowsUnload = function(obj) {
                this._allowComponentUnload(obj.target, obj.fctnYes, obj.fctnNo, obj.args);
            }

            plugin.prototype.detailsDisallowsUnload = function(obj) {
                if(obj.fctnNo) {
                    obj.fctnNo.call(obj.target, obj.args);
                }
            };

            plugin.prototype._allowComponentUnload = function(target, fctnYes, fctnNo, args) {
                if(!this.isEditMode) {
                    fctnYes.call(target, args);
                }
                else {
                    var me = this;
                    tibco.admin.Main.confirm(this.getConfirmTitle(), this.getConfirmText(),
                            function(dialog) {
                                dialog.doClose();
                                me.doCancelEdits();
                                fctnYes.call(target, args);
                            },
                            function(dialog) {
                                dialog.doClose();
                                if(fctnNo) {
                                    fctnNo.call(target, args);
                                }
                            },
                            this.getConfirmYesButtonText(), this.getConfirmNoButtonText(), this.getDefaultConfirmButton());
                }
            }

			plugin.prototype.setSelectDefaultText = function() {
                tibco.admin.Main.getServer().getJSXByName("selEnvironmentSelect_30").setDefaultText(tibco.admin.Main.getServer().getDynamicProperty("@@selectDefaultText"));
            };
        });
